home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-14 | 8.5 KB | 314 lines | [TEXT/KAHL] |
- // Simple framework for Macintosh sample code
- //
- // Nick Thompson, DEVSUPPORT
- //
- // This file contains the appleevent related code code for the framework.
- // Alle we do in here is provide support for the required AppleEvents
- //
- // 9/16/94 nick first cut
- // 10/14/94 nick cleaned up for release
-
- #define kGestaltTrap 0xA0AD
-
- #include <GestaltEqu.h>
-
- #include "Aevt.h"
- #include "MyTraps.h"
- #include "Mtb.h"
- #include "TextMediaExample.h"
-
- // Global scope
- extern Boolean gQuitFlag ;
-
- // The have scope only in this file
-
- static AEAddressDesc pSelfAddress; // A self-addressed address descriptor record
- static ProcessSerialNumber pSelfPSN; // This application's psn
- static AEDesc pNullDesc; // A null descriptor record
-
- //-----------------------------------------------------------------------
- // make sure you call this to init the things used in this file
-
- void InitAEStuff( void )
- {
- // Set up the self-addressed descriptor record.
- pSelfPSN.highLongOfPSN = 0;
- pSelfPSN.lowLongOfPSN = kCurrentProcess; // Use this instead of GetCurrentProcess
- // to allow appleevents to ourself
- // to be directly dispatched ( a
- // subroutine call to our handler )
- // instead of having to be processed
- // through our event loop as events
- // coming in from the outside will be.
-
- CheckError(AECreateDesc( typeProcessSerialNumber,
- (Ptr)&pSelfPSN,
- sizeof(ProcessSerialNumber),
- &pSelfAddress),
- "\pCouldn't create a PSN descriptor for pSelfAddress");
-
- pNullDesc.descriptorType = typeNull; // Initialize the global null descriptor record.
- pNullDesc.dataHandle = nil;
-
- }
-
-
- //-----------------------------------------------------------------------
- // returns true if the platform supports appleevents - we won't run
- // if it doesn't
-
- Boolean SupportsAEVT(void)
- {
- OSErr err;
- long response;
-
- if (!TrapAvailable(kGestaltTrap))
- return false;
-
- err = Gestalt(gestaltAppleEventsAttr,&response);
- if (err!=noErr)
- return false;
-
- return (response && (response << gestaltAppleEventsPresent));
- }
-
- //-----------------------------------------------------------------------
- // called to process high level appleevents
-
- void DoHighLevelEvent(EventRecord *ev)
- {
- OSErr err;
-
- err = AEProcessAppleEvent(ev);
- }
-
-
- //-----------------------------------------------------------------------
- // called to register our appleevent handlers
-
- void RegisterMyEvents(void)
- {
- OSErr err;
-
- if (!SupportsAEVT())
- return;
-
- err = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(MyAEHandleOAPP),0L,false);
- if (err!=noErr)
- return;
-
- err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(MyAEHandleODOC),0L,false);
- if (err!=noErr)
- return;
-
- err = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(MyAEHandlePDOC),0L,false);
- if (err!=noErr)
- return;
-
- err = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(MyAEHandleQUIT),0L,false);
- if (err!=noErr)
- return;
- }
-
- //-----------------------------------------------------------------------
- // open application event handler for the core event suite
-
- pascal OSErr MyAEHandleOAPP(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
- {
- OSErr err = noErr;
-
- return err;
- }
-
-
- //-----------------------------------------------------------------------
- // handler for the open document appleevent handler
-
- pascal OSErr MyAEHandleODOC(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
- {
- FSSpec myFSS;
- AEDescList docList;
- OSErr err,
- ignoreErr;
- long index,
- itemsInList;
- Size actualSize;
- AEKeyword keywd;
- DescType returnedType;
- Size size;
-
-
- err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList);
- if (err == noErr) {
-
- // see how many descriptor items are in the list
- // this is the number of documents we want to open
- err = AECountItems(&docList,&itemsInList);
-
- // now get each descriptor record from the list
- // coerce the returned data to an FSSpec record, and
- // open the asoociated file
-
- for (index=1; index <= itemsInList && err == noErr; index++) {
-
- err = AEGetNthPtr( &docList,
- index,
- typeFSS,
- &keywd,
- &returnedType,
- (Ptr)&myFSS,
- sizeof(myFSS),
- &actualSize);
-
- if (err == noErr) {
- HandleOpenDoc( &myFSS ) ;
- }
- }
- ignoreErr = AEDisposeDesc(&docList);
- }
- return err ;
- }
-
- //-----------------------------------------------------------------------
- // handler for the print document event handler
-
- pascal OSErr MyAEHandlePDOC(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
- {
- FSSpec myFSS;
- AEDescList docList;
- OSErr err;
- long index,
- itemsInList;
- Size actualSize;
- AEKeyword keywd;
- DescType returnedType;
- Size size;
-
- err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList);
- if (err == noErr) {
-
- // see how many descriptor items are in the list
- // this is the number of documents we want to open
- err = AECountItems(&docList,&itemsInList);
-
- // now get each descriptor record from the list
- // coerce the returned data to an FSSpec record, and
- // open the asoociated file
-
- for (index=1; index <= itemsInList && err == noErr; index++) {
-
- err = AEGetNthPtr( &docList,
- index,
- typeFSS,
- &keywd,
- &returnedType,
- (Ptr)&myFSS,
- sizeof(myFSS),
- &actualSize);
-
- if (err == noErr) {
- err = HandlePrintDoc( &myFSS );
- }
- }
- err = AEDisposeDesc(&docList);
- }
- return err ;
- }
-
- //-----------------------------------------------------------------------
- // quit appleevent handler
-
- pascal OSErr MyAEHandleQUIT(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
- {
- OSErr err = noErr;
-
- // close all windows and signal to Quit
- WindowPeek theWindow = nil;
-
- for( theWindow = (WindowPeek)FrontWindow(); theWindow != nil; theWindow = theWindow->nextWindow) {
- DoDestroyMovieWindow((WindowPtr)theWindow) ;
- }
-
- err = AEDisposeDesc(&pSelfAddress); // Dispose of my self-addressed descriptor.
-
- gQuitFlag = true;
- return err ;
- }
-
- //----------------------------------------------------------------------------------//
- // Send a Quit Application Apple Event to myself to terminate this app.
-
- void SendQuitApp( void )
- {
- AppleEvent myAppleEvent, reply;
-
- // Create the Apple Event.
- CheckError(AECreateAppleEvent( kCoreEventClass,
- kAEQuitApplication,
- &pSelfAddress,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &myAppleEvent), "\pCouldn't create a 'quit' AppleEvent");
-
- // Send the Apple Event.
- CheckError(AESend( &myAppleEvent,
- &reply,
- kAENoReply+kAENeverInteract,
- kAENormalPriority,
- kAEDefaultTimeout,
- nil,
- nil), "\pCouldn't send a 'quit' AppleEvent");
-
- AEDisposeDesc(&myAppleEvent); // Dispose of the Apple Event.
- } // SendQuitApp
-
- //----------------------------------------------------------------------------------//
- // Send a Open Document Application Apple Event to myself to open a document.
-
- void SendOpenDoc(FSSpec *myFSSpec)
- {
- AppleEvent myAppleEvent;
- AppleEvent defReply;
- AEDescList docList;
- OSErr myErr;
- OSErr ignoreErr;
-
- myAppleEvent.dataHandle = nil;
- docList.dataHandle = nil;
- defReply.dataHandle = nil;
-
- // Create empty list and add one file spec
- CheckError(AECreateList(nil,0,false, &docList), "\pCouldn't create a list descriptor for an 'odoc' AppleEvent");
-
- CheckError(AEPutPtr(&docList,1,typeFSS,(Ptr)myFSSpec,sizeof(FSSpec)), "\pCouldn't insert the FSSpec for an 'odoc' AppleEvent");
-
- CheckError(AECreateAppleEvent( kCoreEventClass,
- kAEOpenDocuments,
- &pSelfAddress,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &myAppleEvent), "\pCouldn't create an 'odoc' AppleEvent");
-
- // Put Params into our event and send it
-
- CheckError(AEPutParamDesc( &myAppleEvent,
- keyDirectObject,
- &docList), "\pCouldn't package FSSpec for an 'odoc' AppleEvent");
-
- CheckError(AESend( &myAppleEvent,
- &defReply,
- kAENoReply+kAENeverInteract,
- kAENormalPriority,
- kAEDefaultTimeout,
- nil,
- nil), "\pCouldn't send an 'odoc' AppleEvent");
-
-
- if (myAppleEvent.dataHandle)
- ignoreErr = AEDisposeDesc(&myAppleEvent);
-
- if (docList.dataHandle)
- ignoreErr = AEDisposeDesc(&docList);
-
- } // SendOpenDoc
-